package com.learn.kotlinlearndemolist

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import com.google.android.material.tabs.TabLayoutMediator
import com.learn.kotlinlearndemolist.databinding.FragmentMainBinding
import com.learn.kotlinlearndemolist.ui.FirstFragment
import com.learn.kotlinlearndemolist.ui.SecondFragment

class MainFragment : Fragment() {

    private lateinit var mBinding: FragmentMainBinding
    private lateinit var mFragmentList: MutableList<Fragment>

    companion object {
        private const val TAG = "MainFragment"
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        // Inflate the layout for this fragment
        mBinding = DataBindingUtil.inflate(
            LayoutInflater.from(requireContext()),
            R.layout.fragment_main,
            container,
            false
        )
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mFragmentList = mutableListOf()
        mFragmentList.add(FirstFragment())
        mFragmentList.add(SecondFragment())
        val viewPagerAdapter = ViewPagerAdapter(this, mFragmentList)
        mBinding.viewPager.adapter = viewPagerAdapter
        TabLayoutMediator(
            mBinding.tabLayout,
            mBinding.viewPager
        ) { tab, position ->
            when (position) {
                0 -> tab.text = "First"
                1 -> tab.text = "Second"
                else -> null
            }
        }.attach()
    }
}